package Program.AdminUI;

import Program.Information.Admin;
import Program.LoginUI.LoginUI;
import Program.Tool.PictureAlterJPanel;
import Program.Tool.TabbedPaneDef;

import javax.swing.*;
import javax.swing.event.ChangeEvent;
import javax.swing.event.ChangeListener;
import java.awt.*;
import java.awt.event.*;
import java.util.Calendar;

public class AdminUI extends JFrame {
    //用户界面的选项卡
    private JTabbedPane uTabbedPane1, uTabbedPane2;
    //顶端容器
    private JPanel northPanel;
    //按钮容器
    private JPanel JButtonPanel;
    //最小化按钮
    private JButton minButton;
    //关闭按钮
    private JButton closeButton;
    //更换用户按钮
    private JButton changeButton;
    //费用界面按钮
    private JButton feeButton;
    //报修界面按钮
    private JButton repairButton;
    //投诉界面按钮
    private JButton complaintButton;
    //所有用户信息查询按钮
    private JButton lookUsersButton;
    //查看收费记录
    private JPanel lookFeesJPanel;
    //生成账单
    private JPanel addFeeJPanel;
    //查看待处理充值余额的任务
    private JPanel rBalanceJPanel;
    //费用界面
    private OverheadPaneSon1 ohPaneSon1;
    private OverheadPaneSon2 ohPaneSon2;
    private OverheadPaneSon3 ohPaneSon3;
    //报修查看，处理和删除界面
    private RepairPaneSon repairPaneSon;
    //投诉示界面
    private ComplaintPaneSon complaintPaneSon;
    //所有用户信息显示界面
    private UsersPaneSon usersPaneSon;
    //管理员信息显示界面
    private AdminPaneSon adminPaneSon;
    //查看所有用户信息
    private JPanel lookUsersPanel;
    //查看当前管理员信息
    private JPanel lookAdminPanel;
    //报修界面
    private JPanel repairJPanel;
    //投诉界面
    private JPanel complaintJPanel;
    //管理员
    private Admin admin;
    //设置颜色
    Color color = new Color(191, 230, 240);
    //设置字体
    private Font font = new Font("微软雅黑", Font.BOLD, 20);
    //记录鼠标x轴上的位置
    private int mouseAtX = 0;
    //记录鼠标y轴上的位置
    private int mouseAtY = 0;
    //临时按钮
    private JButton tempJButton;
    //获取时间对象
    private Calendar instance = Calendar.getInstance();


    public AdminUI(Admin admin) {
        this.admin = admin;
        //界面初始化
        initJFrame();
        //获取按钮监听器
        ActionListener acListener = getAcListener(this);
        //添加监听器
        closeButton.addActionListener(acListener);
        minButton.addActionListener(acListener);
        changeButton.addActionListener(acListener);
        feeButton.addActionListener(acListener);
        repairButton.addActionListener(acListener);
        complaintButton.addActionListener(acListener);
        lookUsersButton.addActionListener(acListener);
        this.tempJButton = feeButton;
        canDraged(northPanel);
        //让界面显示出来，建议写在最后
        this.setVisible(true);
    }

    //主界面
    private void initJFrame() {
        //设置窗口标题图标
        this.setIconImage(new ImageIcon("Application_images\\管理员界面图标.png").getImage());
        //设置窗口没又修饰框
        this.setUndecorated(true);
        //设置窗口的大小
        this.setSize(900, 800);
        //设置窗口禁止用户调整
        this.setResizable(false);
        //设置窗口居中
        this.setLocationRelativeTo(null);
        //设置窗口没有布局
        this.setLayout(null);
        //设置窗口背景颜色
        this.getContentPane().setBackground(color);
        //设置窗口关闭模式
        this.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

        //加载界面
        initNorthPanel();
        initJButtonPanel();
        this.add(initOverheadPanel());
    }

    private void initNorthPanel() {
        northPanel = new PictureAlterJPanel("Application_images\\headPane.png");
        northPanel.setBounds(0, 0, 900, 60);
        northPanel.setBackground(Color.white);

        //标题标签
        JLabel showJLabel = new JLabel();
        //设置标签位置
        showJLabel.setBounds(20, 0, 600, 60);
        //设置标签字体
        showJLabel.setFont(font);
        String str = "物业管理系统-管理员模式  H1！" + this.admin.getName() + "  今天是 " + getDateStr();
        //设置内容
        showJLabel.setText(str);
        //添加控件
        northPanel.add(showJLabel);

        //按钮赋予的对象
        minButton = new JButton();
        closeButton = new JButton();
        changeButton = new JButton();

        //设置按钮的位置
        minButton.setBounds(786, 5, 50, 50);
        closeButton.setBounds(845, 5, 50, 50);
        changeButton.setBounds(727, 5, 50, 50);
        //取消按钮的焦点框
        minButton.setFocusPainted(false);
        closeButton.setFocusPainted(false);
        changeButton.setFocusPainted(false);
        //取消按钮的边框
        minButton.setBorder(null);
        closeButton.setBorder(null);
        changeButton.setBorder(null);
        //设置按钮背景透明
        minButton.setContentAreaFilled(false);
        closeButton.setContentAreaFilled(false);
        changeButton.setContentAreaFilled(false);
        //设置鼠标样式
        minButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        closeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        changeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));

        //设置按钮不同状态时的图标
        //设置按钮图标
        minButton.setIcon(new ImageIcon("Application_images\\最小化按钮1.png"));  //未选中时
        minButton.setRolloverIcon(new ImageIcon("Application_images\\最小化按钮2.png"));  //选中时
        minButton.setPressedIcon(new ImageIcon("Application_images\\最小化按钮3.png"));  //按下时

        closeButton.setIcon(new ImageIcon("Application_images\\关闭按钮1.png"));  //未选中时
        closeButton.setRolloverIcon(new ImageIcon("Application_images\\关闭按钮2.png"));  //选中时
        closeButton.setPressedIcon(new ImageIcon("Application_images\\关闭按钮3.png"));   //按下时

        changeButton.setIcon(new ImageIcon("Application_images\\change1.png"));
        changeButton.setRolloverIcon(new ImageIcon("Application_images\\change2.png"));
        changeButton.setPressedIcon(new ImageIcon("Application_images\\change3.png"));


        //添加控件
        northPanel.add(changeButton);
        northPanel.add(minButton);
        northPanel.add(closeButton);


        //添加面板
        this.add(northPanel);
    }

    //中间按钮界面
    private void initJButtonPanel() {
        //设置界面格式
        JButtonPanel = new JPanel(null);
        JButtonPanel.setBackground(Color.white);
        JButtonPanel.setBounds(0, 65, 900, 100);
        JButtonPanel.setBorder(BorderFactory.createLineBorder(new Color(127, 255, 212), 3));


        //按钮格式

        //费用
        //费用标签
        JLabel feeJLabel = new JLabel("费  用");
        //设置字体
        feeJLabel.setFont(font);
        //设置位置和大小
        feeJLabel.setBounds(152, 60, 65, 50);
        //费用按钮
        feeButton = new JButton();
        //取消按钮的焦点框
        feeButton.setFocusPainted(false);
        //取消按钮的边框
        feeButton.setBorder(null);
        //设置按钮背景透明
        feeButton.setContentAreaFilled(false);
        //设置鼠标样式
        feeButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        //设置按钮图标
        feeButton.setIcon(new ImageIcon("Application_images\\费用界面按钮1.png"));  //未选中时
        feeButton.setRolloverIcon(new ImageIcon("Application_images\\费用界面按钮2.png"));  //选中时
        //设置位置和大小
        feeButton.setBounds(150, 5, 64, 64);
        //添加控件
        JButtonPanel.add(feeJLabel);
        JButtonPanel.add(feeButton);


        //报修
        //报修标签
        JLabel repairJLabel = new JLabel("报  修");
        //设置字体
        repairJLabel.setFont(font);
        //设置位置和大小
        repairJLabel.setBounds(320, 60, 65, 50);
        //报修按钮
        repairButton = new JButton();
        //取消按钮的焦点框
        repairButton.setFocusPainted(false);
        //取消按钮的边框
        repairButton.setBorder(null);
        //设置按钮背景透明
        repairButton.setContentAreaFilled(false);
        //设置鼠标样式
        repairButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        //设置按钮图标
        repairButton.setIcon(new ImageIcon("Application_images\\报修按钮1.png"));  //未选中时
        repairButton.setRolloverIcon(new ImageIcon("Application_images\\报修按钮2.png"));  //选中时
        //设置位置和大小
        repairButton.setBounds(315, 5, 64, 64);
        //添加控件
        JButtonPanel.add(repairJLabel);
        JButtonPanel.add(repairButton);

        //投诉
        //投诉标签
        JLabel complaintJLabel = new JLabel("投  诉");
        //设置字体
        complaintJLabel.setFont(font);
        //设置位置和大小
        complaintJLabel.setBounds(485, 60, 65, 50);
        //投诉按钮
        complaintButton = new JButton();
        //取消按钮的焦点框
        complaintButton.setFocusPainted(false);
        //取消按钮的边框
        complaintButton.setBorder(null);
        //设置按钮背景透明
        complaintButton.setContentAreaFilled(false);
        //设置鼠标样式
        complaintButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        //设置按钮图标
        complaintButton.setIcon(new ImageIcon("Application_images\\投诉按钮1.png"));  //未选中时
        complaintButton.setRolloverIcon(new ImageIcon("Application_images\\投诉按钮2.png"));  //选中时
        //设置位置和大小
        complaintButton.setBounds(480, 5, 64, 64);
        //添加控件
        JButtonPanel.add(complaintJLabel);
        JButtonPanel.add(complaintButton);


        //查看所有用户信息
        //查看用户信息标签
        JLabel luJLabel = new JLabel("用户信息");
        //设置字体
        luJLabel.setFont(font);
        //设置位置和大小
        luJLabel.setBounds(647, 60, 80, 50);
        //查看用户信息按钮
        lookUsersButton = new JButton();
        //取消按钮的焦点框
        lookUsersButton.setFocusPainted(false);
        //取消按钮的边框
        lookUsersButton.setBorder(null);
        //设置按钮背景透明
        lookUsersButton.setContentAreaFilled(false);
        //设置鼠标样式
        lookUsersButton.setCursor(new Cursor(Cursor.HAND_CURSOR));
        //设置按钮图标
        lookUsersButton.setIcon(new ImageIcon("Application_images\\用户信息查询按钮1.png"));  //未选中时
        lookUsersButton.setRolloverIcon(new ImageIcon("Application_images\\用户信息查询按钮2.png"));  //选中时
        //设置位置和大小
        lookUsersButton.setBounds(655, 5, 64, 64);
        //添加控件
        JButtonPanel.add(luJLabel);
        JButtonPanel.add(lookUsersButton);

        this.add(JButtonPanel);
    }

    //底部费用内容界面
    private JTabbedPane initOverheadPanel() {
        //选项卡赋予对象
        uTabbedPane1 = new JTabbedPane();
        //容器赋予对象
        lookFeesJPanel = new JPanel(null);
        addFeeJPanel = new JPanel(null);
        rBalanceJPanel = new JPanel(null);

        //容器设置背景颜色
        lookFeesJPanel.setBackground(color);
        addFeeJPanel.setBackground(color);
        rBalanceJPanel.setBackground(color);

        //添加选项卡容器，并且设置其中每个选项卡的标签以及其是否可启用
        uTabbedPane1.addTab("收费记录", lookFeesJPanel);
        uTabbedPane1.setEnabledAt(0, true);
        uTabbedPane1.addTab("生成业单", addFeeJPanel);
        uTabbedPane1.setEnabledAt(1, true);
        uTabbedPane1.addTab("充值处理", rBalanceJPanel);
        uTabbedPane1.setEnabledAt(2, true);
        uTabbedPane1.setUI(new TabbedPaneDef());
        uTabbedPane1.setBounds(0, 162, 900, 700);
        uTabbedPane1.setFont(font);
        uTabbedPane1.setForeground(Color.white);

        ohPaneSon1 = new OverheadPaneSon1();
        lookFeesJPanel.add(ohPaneSon1);
        ohPaneSon2 = new OverheadPaneSon2();
        addFeeJPanel.add(ohPaneSon2);
        ohPaneSon3 = new OverheadPaneSon3();
        rBalanceJPanel.add(ohPaneSon3);

        uTabbedPane1.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
                int selectedIndex = sourceTabbedPane.getSelectedIndex();
                if (selectedIndex == 0) {
                    lookFeesJPanel.removeAll();
                    lookFeesJPanel.repaint();
                    ohPaneSon1 = new OverheadPaneSon1();
                    lookFeesJPanel.add(ohPaneSon1);
                } else if (selectedIndex == 1) {
                    addFeeJPanel.removeAll();
                    addFeeJPanel.repaint();
                    ohPaneSon2 = new OverheadPaneSon2();
                    addFeeJPanel.add(ohPaneSon2);
                } else if (selectedIndex == 2) {
                    rBalanceJPanel.removeAll();
                    rBalanceJPanel.repaint();
                    ohPaneSon3 = new OverheadPaneSon3();
                    rBalanceJPanel.add(ohPaneSon3);
                }
            }
        });

        return uTabbedPane1;
    }

    //底部报修内容界面
    private JPanel initRepairPanel() {
        repairJPanel = new JPanel(null);
        repairJPanel.setBackground(Color.white);
        repairJPanel.setBounds(0, 170, 900, 700);
        repairPaneSon = new RepairPaneSon();
        repairJPanel.add(repairPaneSon);
        return repairJPanel;
    }

    //底部投诉内容界面
    private JPanel initComplaintPanel() {
        complaintJPanel = new JPanel(null);
        complaintJPanel.setBackground(Color.white);
        complaintJPanel.setBounds(0, 170, 900, 700);
        complaintPaneSon = new ComplaintPaneSon();
        complaintJPanel.add(complaintPaneSon);
        return complaintJPanel;
    }

    //底部用户信息界面
    public JTabbedPane initUserJPanel(AdminUI adminUI) {
        //选项卡赋予对象
        uTabbedPane2 = new JTabbedPane();
        //容器赋予对象
        lookUsersPanel = new JPanel(null);
        lookAdminPanel = new JPanel(null);
        //容器设置背景颜色
        lookUsersPanel.setBackground(color);
        lookAdminPanel.setBackground(color);

        //添加选项卡容器，并且设置其中每个选项卡的标签以及其是否可启用
        uTabbedPane2.addTab("用户管理", lookUsersPanel);
        uTabbedPane2.setEnabledAt(0, true);
        uTabbedPane2.addTab("管理员信息", lookAdminPanel);
        uTabbedPane2.setEnabledAt(1, true);
        uTabbedPane2.setUI(new TabbedPaneDef());
        uTabbedPane2.setBounds(0, 162, 900, 700);
        uTabbedPane2.setFont(font);
        uTabbedPane2.setForeground(Color.white);

        usersPaneSon = new UsersPaneSon();
        lookUsersPanel.add(usersPaneSon);
        adminPaneSon = new AdminPaneSon(admin,this);
        lookAdminPanel.add(adminPaneSon);

        uTabbedPane2.addChangeListener(new ChangeListener() {
            @Override
            public void stateChanged(ChangeEvent e) {
                JTabbedPane sourceTabbedPane = (JTabbedPane) e.getSource();
                int selectedIndex = sourceTabbedPane.getSelectedIndex();
                if (selectedIndex == 0) {
                    lookUsersPanel.removeAll();
                    lookUsersPanel.repaint();
                    usersPaneSon = new UsersPaneSon();
                    lookUsersPanel.add(usersPaneSon);
                } else if (selectedIndex == 1) {
                    lookAdminPanel.removeAll();
                    lookAdminPanel.repaint();

                    adminPaneSon = new AdminPaneSon(admin,adminUI);
                    lookAdminPanel.add(adminPaneSon);
                }
            }
        });
        return uTabbedPane2;
    }

    //设置窗口移动
    public void canDraged(JPanel p) {
        //创建接收鼠标事件的监听器
        MouseAdapter mouseAdapter = new MouseAdapter() {
            //当鼠标按下时获取窗口的坐标位置
            public void mousePressed(MouseEvent e) {
                mouseAtX = e.getPoint().x;
                mouseAtY = e.getPoint().y;
            }
        };
        MouseMotionAdapter motionAdapter = new MouseMotionAdapter() {
            public void mouseDragged(MouseEvent e) {
                //设置获取拖拽后的位置并窗口的位置
                setLocation((e.getXOnScreen() - mouseAtX), (e.getYOnScreen() - mouseAtY));
            }
        };
        //给顶部容器添加监听事件
        p.addMouseListener(mouseAdapter);
        p.addMouseMotionListener(motionAdapter);
    }

    public ActionListener getAcListener(AdminUI adminUI) {
        ActionListener listener = new ActionListener() {

            @Override
            public void actionPerformed(ActionEvent e) {
                if (e.getSource() == closeButton) {
                    System.exit(0);
                } else if (e.getSource() == minButton) {
                    setExtendedState(JFrame.ICONIFIED);
                } else if (e.getSource() == changeButton) {
                    SwingUtilities.invokeLater(() -> new LoginUI());
                    dispose();
                } else if (e.getSource() == feeButton) {
                    feeButton.setIcon(new ImageIcon("Application_images\\费用界面按钮3.png"));
                    repairButton.setIcon(new ImageIcon("Application_images\\报修按钮1.png"));
                    complaintButton.setIcon(new ImageIcon("Application_images\\投诉按钮1.png"));
                    lookUsersButton.setIcon(new ImageIcon("Application_images\\用户信息查询按钮1.png"));
                    delJPanel();
                    add(initOverheadPanel());
                    tempJButton = feeButton;
                } else if (e.getSource() == repairButton) {
                    feeButton.setIcon(new ImageIcon("Application_images\\费用界面按钮1.png"));
                    repairButton.setIcon(new ImageIcon("Application_images\\报修按钮3.png"));
                    complaintButton.setIcon(new ImageIcon("Application_images\\投诉按钮1.png"));
                    lookUsersButton.setIcon(new ImageIcon("Application_images\\用户信息查询按钮1.png"));
                    delJPanel();
                    add(initRepairPanel());
                    tempJButton = repairButton;
                } else if (e.getSource() == complaintButton) {
                    feeButton.setIcon(new ImageIcon("Application_images\\费用界面按钮1.png"));
                    repairButton.setIcon(new ImageIcon("Application_images\\报修按钮1.png"));
                    complaintButton.setIcon(new ImageIcon("Application_images\\投诉按钮3.png"));
                    lookUsersButton.setIcon(new ImageIcon("Application_images\\用户信息查询按钮1.png"));
                    delJPanel();
                    add(initComplaintPanel());
                    tempJButton = complaintButton;
                } else if (e.getSource() == lookUsersButton) {
                    feeButton.setIcon(new ImageIcon("Application_images\\费用界面按钮1.png"));
                    repairButton.setIcon(new ImageIcon("Application_images\\报修按钮1.png"));
                    complaintButton.setIcon(new ImageIcon("Application_images\\投诉按钮1.png"));
                    lookUsersButton.setIcon(new ImageIcon("Application_images\\用户信息查询按钮3.png"));
                    delJPanel();
                    add(initUserJPanel(adminUI));
                    tempJButton = lookUsersButton;
                }
            }
        };
        return listener;
    }

    private void delJPanel() {
        if (tempJButton == feeButton) {
            remove(uTabbedPane1);
            repaint();
        } else if (tempJButton == repairButton) {
            remove(repairJPanel);
            repaint();
        } else if (tempJButton == complaintButton) {
            remove(complaintJPanel);
            repaint();
        } else if (tempJButton == lookUsersButton) {
            remove(uTabbedPane2);
            repaint();
        }
    }


    //获取时间
    private String getDateStr() {
        //3.获取属性
        int year = instance.get(Calendar.YEAR);
        int month = instance.get(Calendar.MONTH) + 1;//Calendar的月份值是0-11
        int day = instance.get(Calendar.DAY_OF_MONTH);

        return year + "/" + month + "/" + day;
    }
}
